﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TDT_Project.classes;
namespace TDT_Project.Controller
{
    public partial class xem_nhanxet_admin : System.Web.UI.UserControl
    {
        cls_nienkhoa nienche = new cls_nienkhoa();
        cls_lop lop = new cls_lop();
        cls_phuhuynhxemdiem cls = new cls_phuhuynhxemdiem();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load_nienche();
                load_lop();
               // load_svien();
                DataTable dt = new DataTable();
                rid_gv.DataSource = dt;
                rid_gv.DataBind();
                DataTable dt1 = new DataTable();
                rid_ph.DataSource = dt1;
                rid_ph.DataBind();
            }
        }
        private void load_nienche()
        {
            DataSet ds = nienche.load_nienkho_combo();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                cbo_nienhoc.DataSource = ds;
                cbo_nienhoc.DataTextField = "TenNienHoc";
                cbo_nienhoc.DataValueField = "NienHoc_id";
                cbo_nienhoc.DataBind();
                string s = "";
                if (DateTime.Now.Month > 8)
                    s = DateTime.Now.Year + "-" + DateTime.Now.AddYears(1).Year;
                else
                    s = DateTime.Now.AddYears(-1).Year + "-" + DateTime.Now.Year;
                DataRow[] dr = ds.Tables[0].Select("TenNienHoc='" + s + "'");
                if (dr.Count() > 0)
                    cbo_nienhoc.SelectedValue = dr[0][0].ToString();
            }
        }
        private void load_lop()
        {
            DataSet ds = lop.load_lop_combo();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                cbo_lop.DataSource = ds;
                cbo_lop.DataTextField = "TenLop";
                cbo_lop.DataValueField = "Lop_Id";
                cbo_lop.DataBind();

            }
        }
        //private void load_svien()
        //{
        //    string lop_id = cbo_lop.SelectedValue.ToString();
        //    string nienkhoa_id = cbo_nienhoc.SelectedValue.ToString();
        //    DataSet ds_hsinh = lop.load_hocsinh_combo(nienkhoa_id, lop_id);
        //    if (ds_hsinh != null && ds_hsinh.Tables[0].Rows.Count > 0)
        //    {
        //        cbo_hocsinh.DataSource = ds_hsinh;
        //        cbo_hocsinh.DataTextField = "hoten";
        //        cbo_hocsinh.DataValueField = "HocSinh_ID";
        //        cbo_hocsinh.DataBind();
        //    }
        //}

        //protected void btn_gopy_PH_Click(object sender, EventArgs e)
        //{
        //    GetNhanXet_GV();
        //    GetNhanXet_PH();
        //}
        private void GetNhanXet_GV()
        {
           
                DataTable dt = new DataTable();
                DataSet ds = cls.Get_NhanXet_GV_Admin(rdo_HK.SelectedValue.ToString(), cbo_nienhoc.SelectedValue.ToString(), cbo_lop.SelectedValue.ToString());
                if (ds != null && ds.Tables.Count > 0)
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        rid_gv.DataSource = ds.Tables[0].DefaultView;
                        rid_gv.DataBind();
                    }
                    else
                    {
                        rid_gv.DataSource = dt;
                        rid_gv.DataBind();

                    }
                else
                {
                    rid_gv.DataSource = dt;
                    rid_gv.DataBind();

                }

           
        }
        private void GetNhanXet_PH()
        {
           
                DataTable dt = new DataTable();
                DataSet ds = cls.Get_NhanXet_HS_Admin(rdo_HK.SelectedValue.ToString(), cbo_nienhoc.SelectedValue.ToString(), cbo_lop.SelectedValue.ToString());
                if (ds != null && ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        rid_ph.DataSource = ds.Tables[0].DefaultView;
                        rid_ph.DataBind();
                    }
                    else
                    {
                        rid_ph.DataSource = dt;
                        rid_ph.DataBind();
                    }
                }
                else
                {
                    rid_ph.DataSource = dt;
                    rid_ph.DataBind();
                }

            
        }

        protected void cbo_nienhoc_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
           // load_svien();
        }

        protected void cbo_lop_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GetNhanXet_GV();
            GetNhanXet_PH();
        }

        protected void rdo_HK_SelectedIndexChanged(object sender, EventArgs e)
        {
           // load_svien();
            GetNhanXet_GV();
            GetNhanXet_PH();
        }
        protected void rid_ph_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
               
                string cmd = e.CommandName;
                int iRow = e.Item.ItemIndex;
                if (cmd == "EditDelItem")
                {
                    string ID = rid_ph.Items[iRow]["Nhan_Xet_ID"].Text;
                    cls.Delete_NhanXet(ID);
                    DataTable dt = new DataTable();
                    DataSet ds = cls.Get_NhanXet_HS_Admin(rdo_HK.SelectedValue.ToString(), cbo_nienhoc.SelectedValue.ToString(), cbo_lop.SelectedValue.ToString());
                    if (ds != null && ds.Tables.Count > 0)
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            rid_ph.DataSource = ds.Tables[0].DefaultView;
                            rid_ph.DataBind();
                        }
                        else
                        {
                            rid_ph.DataSource = dt;
                            rid_ph.DataBind();

                        }
                    else
                    {
                        rid_gv.DataSource = dt;
                        rid_gv.DataBind();

                    }

                }
               
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}